package com.crealth.handler;

import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import com.crealth.ConstantDefine;
import com.crealth.db.CompanyDb;
import com.crealth.db.UserDb;
import com.crealth.model.UserModel;
import com.crealth.msg.NetMessageData;

public class ResponseMsgQueue {
	/*
	private static ExecutorService responseExecutor = null;
	public static void setResponseExecutor(ExecutorService executor) {
		responseExecutor = executor;
	}
	*/
	
	private static ConcurrentLinkedDeque<NetMessageData> responseDeque = 
			new ConcurrentLinkedDeque<NetMessageData>();
	//private static ConcurrentHashMap<Integer, ConcurrentLinkedDeque<NetMessageData> > userResponseDequeMap = 
			//new ConcurrentHashMap<Integer, ConcurrentLinkedDeque<NetMessageData> >();
	
	public static void addMsgToQueue(NetMessageData sendMsg) {
		if (sendMsg == null) {
			return;
		}
		
		responseDeque.addLast(sendMsg);
		//responseExecutor.execute(new ResponseMsgThread());
	}
	
	public static NetMessageData getResponseMsg() {			
		NetMessageData msg = responseDeque.pollFirst();
		
		return msg;
	}
	
	/*
	public static void clearUserMsgDeque(int userID) {
		if ( !userResponseDequeMap.containsKey(userID) ) {
			return ;
		}
		
		ConcurrentLinkedDeque<NetMessageData> responseDeque = null;
		responseDeque = userResponseDequeMap.get(userID);
		responseDeque.clear();
	}*/
	
	
	public static void addMsgToQueue(int cmd, int userID, NetMessageData sendMsg) {
		if (sendMsg == null || userID <= 0) {
			return;
		}
		NetMessageData msg = new NetMessageData();
		msg.setUserID(userID);
		msg.setMsgCmd(cmd);
		msg.setMsgBody(sendMsg.getMsgBody());
		addMsgToQueue(msg);
	}
	
	public static void sendBranchCompanyMgr(int opUser, int companyID, NetMessageData sendMsg) {
		List<UserModel> branchMgr = UserDb.getBranchCompanyMgr(companyID);
		for (UserModel user : branchMgr) {
			if (opUser == user.getId()) {
				continue;
			}
			NetMessageData msg = new NetMessageData();
			msg.setMsgCmd(sendMsg.getMsgCmd());
			msg.setUserID(user.getId());
			msg.setMsgBody(sendMsg.getMsgBody());
			
			addMsgToQueue(msg);
		}		
	}
	
	public static void sendMsgToCompany(int opUser, int companyID, NetMessageData sendMsg) {
		List<UserModel> companyUserls = UserDb.selectBranchUser(companyID);
		for (UserModel user : companyUserls) {
			if (opUser == user.getId()) {
				continue;
			}
			NetMessageData msg = new NetMessageData();
			msg.setMsgCmd(sendMsg.getMsgCmd());
			msg.setUserID(user.getId());
			msg.setMsgBody(sendMsg.getMsgBody());
			
			addMsgToQueue(msg);
		}		
	}
	
	public static void sendCompanyTopMgr(int opUser, NetMessageData sendMsg) {
		List<UserModel> topMgr = UserDb.selectCompanyTopManager();
		for (UserModel user : topMgr) {
			if (opUser == user.getId()) {
				continue;
			}
			NetMessageData msg = new NetMessageData();
			msg.setMsgCmd(sendMsg.getMsgCmd());
			msg.setUserID(user.getId());
			msg.setMsgBody(sendMsg.getMsgBody());
			
			addMsgToQueue(msg);
		}		
	}
	
	//  发消息给客服
	public static void sendMsgToCustomerService(int opUser, int companyID, NetMessageData sendMsg) {
		String strDepartmentIDs = CompanyDb.getDepartmentIDs(companyID, true);
		List<UserModel> userls = UserDb.selectCompanyUsersByPosition(ConstantDefine.Customer_Service);
		for (UserModel user : userls) {
			if (opUser == user.getId()) {
				continue;
			}
			String userCompanyID = user.getCompanyID() + "";
			if (strDepartmentIDs.indexOf(userCompanyID) < 0) {
				continue;
			}
			NetMessageData msg = new NetMessageData();
			msg.setMsgCmd(sendMsg.getMsgCmd());
			msg.setUserID(user.getId());
			msg.setMsgBody(sendMsg.getMsgBody());
			
			addMsgToQueue(msg);
		}
	}
	
	//  发消息给财务
	public static void sendMsgToFinance(int opUser, int companyID, NetMessageData sendMsg) {
		/*
		CompanyModel company = CompanyCached.selectCompanyByID(companyID);
		if (company == null) {
			return;
		}
		company = company.getTopCompany();
		String strDepartmentIDs = CompanyDb.getDepartmentIDs(companyID, true);
		*/
		List<UserModel> userls = UserDb.selectCompanyUsersByPosition(ConstantDefine.Financer);
		for (UserModel user : userls) {
			if (opUser == user.getId()) {
				continue;
			}
			NetMessageData msg = new NetMessageData();
			msg.setMsgCmd(sendMsg.getMsgCmd());
			msg.setUserID(user.getId());
			msg.setMsgBody(sendMsg.getMsgBody());
			
			addMsgToQueue(msg);
		}
	}
	
	//  发消息给行政
	public static void sendMsgToAdministration(int opUser, int companyID, NetMessageData sendMsg) {
		List<UserModel> userls = UserDb.selectCompanyUsersByPosition(ConstantDefine.Administration);
		for (UserModel user : userls) {
			if (opUser == user.getId()) {
				continue;
			}
			NetMessageData msg = new NetMessageData();
			msg.setMsgCmd(sendMsg.getMsgCmd());
			msg.setUserID(user.getId());
			msg.setMsgBody(sendMsg.getMsgBody());
			
			addMsgToQueue(msg);
		}
	}
	
	//  发消息给团队组员
	public static void sendMsgToTream(int teamID, NetMessageData sendMsg, boolean containTeamMgr) {		
		List<UserModel> userls = UserDb.selectTeamUserByTeamID(teamID);
		if (userls == null || userls.size() == 0) {
			return;
		}		
		for (UserModel user : userls) {
			if (!containTeamMgr && 
				user.getPosition() == ConstantDefine.Team_Manager ) {
				continue;
			}
			NetMessageData msg = new NetMessageData();
			msg.setMsgCmd(sendMsg.getMsgCmd());
			msg.setUserID(user.getId());
			msg.setMsgBody(sendMsg.getMsgBody());
			
			addMsgToQueue(msg);
		}
	}
	
	//  发消息给 按揭
	public static void sendMsgToMortgager(int opUser, int companyID, NetMessageData sendMsg) {
		List<UserModel> userls = UserDb.selectCompanyUsersByPosition(ConstantDefine.Installmenter);
		for (UserModel user : userls) {
			if (opUser == user.getId()) {
				continue;
			}
			NetMessageData msg = new NetMessageData();
			msg.setMsgCmd(sendMsg.getMsgCmd());
			msg.setUserID(user.getId());
			msg.setMsgBody(sendMsg.getMsgBody());
			
			addMsgToQueue(msg);
		}
	}
	
	//  发消息给 按揭部经理
	public static void sendMsgToMortgagerMgr(int opUser, int companyID, NetMessageData sendMsg) {
		List<UserModel> userls = UserDb.selectCompanyUsersByPosition(ConstantDefine.InstallDpmMGR);
		for (UserModel user : userls) {
			if (opUser == user.getId()) {
				continue;
			}
			NetMessageData msg = new NetMessageData();
			msg.setMsgCmd(sendMsg.getMsgCmd());
			msg.setUserID(user.getId());
			msg.setMsgBody(sendMsg.getMsgBody());
			
			addMsgToQueue(msg);
		}
	}
	
	//  发消息给 客户经理或者小组长、团队经理
	public static void sendMsgToSalesMan(int opUser, NetMessageData sendMsg) {
		List<UserModel> userls = UserDb.selectCompanyUsersByPosition(ConstantDefine.Sales_Man);
		List<UserModel> groupLeaderls = UserDb.selectCompanyUsersByPosition(ConstantDefine.Group_Leader);
		if (groupLeaderls != null && groupLeaderls.size() > 0) {
			for (UserModel curUser : groupLeaderls) {
				userls.add(curUser);
			}
		}
		List<UserModel> teamMgrls = UserDb.selectCompanyUsersByPosition(ConstantDefine.Team_Manager);
		if (teamMgrls != null && teamMgrls.size() > 0) {
			for (UserModel curUser : teamMgrls) {
				userls.add(curUser);
			}
		}
		for (UserModel user : userls) {
			if (opUser == user.getId()) {
				continue;
			}
			NetMessageData msg = new NetMessageData();
			msg.setMsgCmd(sendMsg.getMsgCmd());
			msg.setUserID(user.getId());
			msg.setMsgBody(sendMsg.getMsgBody());
			
			addMsgToQueue(msg);
		}
	}
	
}
